package com.mail.server.controller;


import com.mail.model.db.UmsResource;
import com.mail.model.db.UmsResourceCategory;
import com.mail.model.dto.ResouceDto;
import com.mail.model.vo.MyPage;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import com.mail.server.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author WJZ
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 查询所有后台资源
     * 接口地址:/resource/listAll
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<UmsResource>> listAll() {
        List<UmsResource> umsResources = umsResourceService.listAll();
        return R.success(umsResources, "操作成功!");
    }

    /**
     * 分页模糊查询后台资源
     * 接口地址:/resource/list
     *
     * @return
     */
    @GetMapping("/list")
    public R<MyPage<UmsResource>> list(@RequestParam(defaultValue = "1") Long pageNum,
                                       @RequestParam(defaultValue = "10") Long pageSize,
                                       Long categoryId,
                                       String nameKeyword,
                                       String urlKeyword) {
        MyPage<UmsResource> umsResources = umsResourceService.listByPage(pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
        return R.success(umsResources, "操作成功!");
    }

    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public R createResource(@RequestBody UmsResource umsResource) {
        umsResourceService.createResource(umsResource);
        return R.success(null);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteResourceById(@PathVariable Integer id) {
        umsResourceService.deleteResourceById(Long.valueOf(id));
        return R.success(null);
    }

    /**
     * 修改后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateResource(@PathVariable Integer id, @RequestBody UmsResource umsResource) {
        umsResourceService.updateResource(Long.valueOf(id), umsResource);
        return R.success(null);
    }

    /**
     * 根据ID获取资源详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R queryResourceById(@PathVariable Integer id) {
        UmsResource umsResource = umsResourceService.queryResourceById(Long.valueOf(id));
        return R.success(umsResource);
    }
}
